import type {
  AjaxPagingResult,
  AjaxTimeIntervalDateParams,
  DeepPartial,
  SchedulingSaveOrUpdateShiftUnitParams,
  SchedulingSelectSchedulingPlanPageParams,
  SchedulingSelectSchedulingPlanPageResult,
  SchedulingSelectShiftUnitListParams,
  SchedulingShiftUnitInfo,
} from '@/types'

const controller = getAjaxBaseUrl('/scheduling', '/energy')

/** 查询班次单元列表 */
export function selectShiftUnitList(params: SchedulingSelectShiftUnitListParams) {
  return get(`${controller}/selectShiftUnitList`, params)
}

/** 新增或修改班次单元 */
export function saveOrUpdateShiftUnit(params: DeepPartial<SchedulingSaveOrUpdateShiftUnitParams>) {
  return post<SchedulingShiftUnitInfo>(
    `${controller}/saveOrUpdateShiftUnit`,
    params,
  )
}

/** 根据id删除班次单元 */
export function deleteShiftUnitById(id: number) {
  return get(`${controller}/deleteShiftUnitById`, { id })
}

/** 分页查询排版计划信息 */
export function selectSchedulingPlanPage(params: Partial<SchedulingSelectSchedulingPlanPageParams>) {
  return post<SchedulingSelectSchedulingPlanPageResult>(
    `${controller}/selectSchedulingPlanPage`,
    params,
  )
}

/** 导出排班计划信息 */
export async function exportSchedulingPlanData(params: Partial<AjaxTimeIntervalDateParams>) {
  const { data } = await request({
    url: `${controller}/exportSchedulingPlanData`,
    params,
    method: 'get',
    responseType: 'blob',
  })
  downloadByA(
    URL.createObjectURL(data),
    `排班计划_${formatTime(
      params.startTime,
      'YYYY-MM-DD',
    )}_${formatTime(params.endTime, 'YYYY-MM-DD')}`,
  )
}

/** 创建排班计划入参 */
export interface ScheuldingSaveSchedulingPlanParams {
  year: string
  schedulingSchemeName: string
  schedulingPlanList: {
    shiftName: string
    teamName: string
    startTime?: string
    endTime?: string
    /** 工时 */
    totalTimeMinute: number
    /** 是否跨天 */
    overnightShift: 0 | 1
  }[]
}

/** 创建排班计划 */
export function saveSchedulingPlan(data: ScheuldingSaveSchedulingPlanParams) {
  return post(`${controller}/saveSchedulingPlan`, data)
}

/** 分页查询排班方案 */
export function selectSchedulingSchemePage(data: {
  pageNumber: number
  pageSize: number
  schedulingSchemeName?: string
  shiftUnitName?: string
  teamName?: string
}) {
  return post<AjaxPagingResult>(
    `${controller}/selectSchedulingSchemePage`,
    data,
  )
}

/** 新增或修改排班 */
export function saveOrUpdateSchedulingScheme(data: {
  schedulingScheme: {
    id?: number
    name: string
    shiftUnitId: number
  }
  teamIdList: number[]
}) {
  return post(`${controller}/saveOrUpdateSchedulingScheme`, data)
}

/** 删除排班方案 */
export function deleteSchedulingSchemeById(id: number) {
  return get(`${controller}/deleteSchedulingSchemeById`, {
    id,
  })
}

/** 新增或修改班组 */
export function saveOrUpdateTeam(data: {
  id?: number
  name: string
}) {
  return post(`${controller}/saveOrUpdateTeam`, data)
}

/** 分页查询班组 */
export function selectTeamPage(data: {
  pageNumber: number
  pageSize: number
  name?: string
}) {
  return post<AjaxPagingResult>(
    `${controller}/selectTeamPage`,
    data,
  )
}

/** 删除班组 */
export function deleteTeamById(id: number) {
  return get(`${controller}/deleteTeamById`, {
    id,
  })
}
